package com.zp.game

import android.graphics.Bitmap
import android.os.Handler
import android.os.Looper
import androidx.activity.viewModels
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.tools.BitmapMemCache
import com.helloandroid.tools.MyLog
import game.engine.base.GameActivity

class ZpGame : GameActivity() {

    class Res{
        lateinit var bgBitmap:Bitmap
        lateinit var titleBitmap:Bitmap
        lateinit var backBitmap:Bitmap
        lateinit var helpBitmap:Bitmap
        lateinit var pieceBitmap:Bitmap

        lateinit var lightBitmap1:Bitmap
        lateinit var lightBitmap2:Bitmap
        lateinit var panziBitmap:Bitmap
        lateinit var panziBgBitmap:Bitmap
        lateinit var startBitmap:Bitmap
        lateinit var textBgBitmap: Bitmap

        // 额外奖励
        lateinit var extraBgBitmap: Bitmap
        lateinit var activeBitmap1:Bitmap
        lateinit var activeBitmap2:Bitmap
        lateinit var activeBitmap5: Bitmap

        lateinit var inActiveBitmap1:Bitmap
        lateinit var inActiveBitmap2:Bitmap
        lateinit var inActiveBitmap5: Bitmap

        lateinit var pgBgBitmap:Bitmap
        lateinit var pgFgBitmap: Bitmap

        lateinit var alreadyGetBitmap:Bitmap

        var bigTextSize = 42f

        fun loadRes() {
            bgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_bg)
            titleBitmap = BitmapMemCache.initBitmap(R.drawable.zp_title)
            backBitmap = BitmapMemCache.initBitmap(R.drawable.dd_btn_back_light)
            helpBitmap = BitmapMemCache.initBitmap(R.drawable.zp_help)
            pieceBitmap = BitmapMemCache.initBitmap(R.drawable.zp_pieces)
            lightBitmap1 = BitmapMemCache.initBitmap(R.drawable.zp_light_1)
            lightBitmap2 = BitmapMemCache.initBitmap(R.drawable.zp_light_2)
            panziBitmap  = BitmapMemCache.initBitmap(R.drawable.zp_panzi)
            panziBgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_panzi_bg)
            startBitmap = BitmapMemCache.initBitmap(R.drawable.zp_start)
            textBgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_text_bg)

            extraBgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_extra_bg)
            activeBitmap1 = BitmapMemCache.initBitmap(R.drawable.zp_active_1)
            activeBitmap2 = BitmapMemCache.initBitmap(R.drawable.zp_active_2)
            activeBitmap5 = BitmapMemCache.initBitmap(R.drawable.zp_active_5)
            inActiveBitmap1 = BitmapMemCache.initBitmap(R.drawable.zp_inactive_1)
            inActiveBitmap2 = BitmapMemCache.initBitmap(R.drawable.zp_inactive_2)
            inActiveBitmap5 = BitmapMemCache.initBitmap(R.drawable.zp_inactive_5)

            pgBgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_pg_bg)
            pgFgBitmap = BitmapMemCache.initBitmap(R.drawable.zp_pg_fg)

            alreadyGetBitmap = BitmapMemCache.initBitmap(R.drawable.zp_already_get)

            //bigTextSize = MyApplication.app.resources.getDimension(R.dimen.sp14)
            //MyLog.elog("bigTextSize:${bigTextSize}")
        }
    }

    companion object{
        val res = Res()
    }

    val handler = Handler(Looper.myLooper()!!)

    @Volatile
    var isReady = false

    var mInfo : ZhuanPanInfo? = null
    val model : ZpViewModel by viewModels()

    override fun getContentViewResId(): Int = R.layout.activity_zpgame

    override fun launchGame() {
        res.loadRes()
        director.startScene(ZpScene())
    }

    override fun initData() {
        MyLog.elog("=====ZpGame initData called!")
        model.getInfo { success, zhuanPanInfo ->
            isReady = success
            if (success){
                mInfo = zhuanPanInfo

                // 投递一个方法 在游戏线程中去执行......
                director.mainThreadEvent.add{scene->
                    MyLog.elog("============== 开始更新UI罗....")
                    if (scene is ZpScene){
                        scene.updateUI(mInfo!!)
                    }
                }
            }else{
                AppUtil.toast("获取转盘信息失败!")
            }
        }
    }

    override fun showResult(coins: Int) {
    }
}